home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Leser 19 / Amiga Plus Leser CD 19.iso / Online / AmigaTalk / general / Collection.st < prev    next >
Text File  |  2002-06-10  |  3KB  |  115 lines

  1. Class Collection :Object
  2. [
  3.    addAll: aCollection
  4.      aCollection do: [:x | self add: x ]
  5. |
  6.    asArray ! mySize !
  7.       mySize <- self size.
  8.       
  9.       ^ Array new: mySize ; replaceFrom: 1 to: mySize with: self
  10. |
  11.    asBag
  12.       ^ Bag new addAll: self
  13. |
  14.    asSet
  15.       ^ Set new addAll: self
  16. |
  17.    asList
  18.       ^ List new addAllLast: self
  19. |
  20.    asString ! mySize !
  21.       mySize <- self size.
  22.       
  23.       ^ String new: mySize ; replaceFrom: 1 to: mySize with: self
  24. |
  25.    coerce: aCollection   ! newobj !
  26.       newobj <- self new.
  27.  
  28.       aCollection do: [:x | newobj add: x].
  29.  
  30.       ^ newobj
  31. |
  32.    collect: aBlock
  33.       ^ self inject: self class new
  34.                into: [:x :y | x add: (aBlock value: y). x ]
  35. |
  36.    deepCopy     ! newobj !
  37.       newobj <- List new .
  38.  
  39.       self do: [:x | newobj addLast: x copy ].
  40.  
  41.       ^ self coerce: newobj
  42. |
  43.    detect: aBlock
  44.       ^ self detect: aBlock
  45.            ifAbsent: [self error: 'no object found matching detect']
  46. |
  47.    detect: aBlock ifAbsent: exceptionBlock   
  48.       self do: [:x | (aBlock value: x) 
  49.                      ifTrue: [ ^ x ] ].
  50.      
  51.       ^ exceptionBlock value
  52. |
  53.    first
  54.       ^ self error: 'subclass should implement first'
  55. |
  56.    includes: anObject
  57.       self do: [:x | (x = anObject) ifTrue: [ ^ true ] ].
  58.  
  59.       ^ false
  60. |
  61.    inject: thisValue into: binaryBlock     ! last !
  62.       last <- thisValue.
  63.  
  64.       self do: [:x | last <- binaryBlock value: last value: x].
  65.  
  66.       ^ last
  67. |
  68.    isEmpty
  69.       ^ (self size = 0)
  70. |
  71.    occurrencesOf: anObject
  72.       ^ self inject: 0
  73.                into: [:x :y | (y = anObject) 
  74.                       ifTrue: [x + 1]
  75.                       ifFalse: [x] ]
  76. |
  77.    printString
  78.       ^ ( self inject: self class printString , ' ('
  79.           into: [:x :y | x , ' ' , y printString]), ' )'
  80. |
  81.    reject: aBlock          
  82.       ^ self select: [:x | (aBlock value: x) not ]
  83. |
  84.    remove: oldObject
  85.       self remove: oldObject 
  86.          ifAbsent: [^ self error: 'attempt to remove object not found in collection' ].
  87.  
  88.       ^ oldObject
  89. |
  90.    remove: oldObject ifAbsent: exceptionBlock
  91.       ^ (self includes: oldObject)
  92.           ifTrue: [self remove: oldObject]
  93.          ifFalse: exceptionBlock
  94. |
  95.    select: aBlock          
  96.       ^ self inject: self class new
  97.                into: [:x :y | (aBlock value: y) 
  98.                                 ifTrue: [x add: y]. 
  99.                               x ]
  100. |
  101.    shallowCopy  ! newobj !
  102.       newobj <- List new.
  103.  
  104.       self do: [:x | newobj addLast: x].
  105.  
  106.       ^ self coerce: newobj
  107. |
  108.    size ! i !
  109.       i <- 0.
  110.  
  111.       self do: [:x | i <- i + 1 ].
  112.  
  113.       ^ i
  114. ]
  115.